home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / Macintosh Tracker Folder / Tracker Server Folder / open.c < prev    next >
Text File  |  1993-05-18  |  5KB  |  216 lines

  1. /* open.c */
  2.  
  3. /* Magic open file: path lookup and transparent decompression */
  4.  
  5. /* $Id: open.c,v 3.2 1992/12/03 15:00:50 espie Exp espie $ 
  6.  * $Log: open.c,v $
  7.  * Revision 3.2  1992/12/03  15:00:50  espie
  8.  * restore stty.
  9.  *
  10.  * Revision 3.1  1992/11/19  20:44:47  espie
  11.  * Protracker commands.
  12.  *
  13.  * Revision 3.0  1992/11/18  16:08:05  espie
  14.  * New release.
  15.  *
  16.  * Revision 1.5  1992/11/01  13:10:06  espie
  17.  * Cleaned up path handler, and some more bugs.
  18.  * Check for size now.
  19.  * Added path support. Transparent interface. We look up through the file
  20.  * list, which is small anyway.
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <malloc.h>
  27.  
  28. #include "defs.h"
  29. #include "extern.h"
  30.  
  31. LOCAL char *id = "$Id: open.c,v 3.2 1992/12/03 15:00:50 espie Exp espie $";
  32.  
  33. #define MAX_DESC 50 /* Maximum number of opened files */
  34.  
  35. LOCAL struct exfile 
  36.     {
  37.     FILE *handle;
  38.     int type;
  39.     } desc[MAX_DESC];
  40.  
  41. LOCAL int ixfile = 0;
  42.  
  43. #define REALFILE 1
  44. #define PIPEFILE 2
  45.  
  46. /* compression methods we do know about.
  47.  * Important restriction: for the time being, the output
  48.  * must be a single module.
  49.  */
  50.  
  51. LOCAL struct compression_method
  52.     {
  53.     char *extension;
  54.     char *command;
  55.     } comp_table[] =
  56.     {
  57.     ".Z",   "zcat %s",
  58.     ".zoo", "zoo xpq %s",
  59.     ".lzh", "lha pq %s",
  60.     ".lha", "lha pq %s",
  61.     ".zip", "unzip -pqq %s",
  62.     ".arc", "arc pn %s",
  63.     NULL,   NULL
  64.     };
  65.  
  66. /***
  67.  *
  68.  *  Handling extensions.
  69.  *
  70.  ***/
  71.  
  72. LOCAL BOOL check_ext(s, ext)
  73. char *s, *ext;
  74.     {
  75.     int ext_len, s_len;
  76.     char *c;
  77.  
  78.     ext_len = strlen(ext);
  79.     s_len = strlen(s);
  80.     if (s_len < ext_len)
  81.         return FALSE;
  82.     for (c = s + s_len - ext_len; *c; c++, ext++)
  83.         if (tolower(*c) != tolower(*ext))
  84.             return FALSE;
  85.     return TRUE;
  86.     }
  87.  
  88. LOCAL BOOL exist_file(fname)
  89. char *fname;
  90.     {
  91.     FILE *temp;
  92.  
  93.     temp = fopen(fname, "r");
  94.     if (temp)
  95.         {
  96.         fclose(temp);
  97.         return TRUE;
  98.         }
  99.     else
  100.         return FALSE;
  101.     }
  102.  
  103. #ifndef MAXPATHLEN
  104. #define MAXPATHLEN 350
  105. #endif
  106.  
  107. LOCAL char *find_file(fname, path)
  108. char *fname;
  109. char *path;
  110.     {
  111.     char *sep;
  112.     static char buffer[MAXPATHLEN];
  113.     int len;
  114.  
  115.         /* first, check the current directory */
  116.     if (exist_file(fname))
  117.         return fname;
  118.     while(path)
  119.         {
  120.         sep = strchr(path, ':');
  121.         if (sep)
  122.             len = sep - path;
  123.         else
  124.             len = strlen(path);
  125.         if (len < MAXPATHLEN)
  126.             {
  127.             strncpy(buffer, path, len);
  128.             buffer[len] = '/';
  129.             if (len + strlen(fname) < MAXPATHLEN - 5)
  130.                 {
  131.                 strcpy(buffer + len + 1, fname);
  132.                 puts(buffer);
  133.                 if (exist_file(buffer))
  134.                     return buffer;
  135.                 }
  136.             }
  137.         if (sep)
  138.             path = sep + 1;
  139.         else
  140.             return NULL;
  141.         }
  142.     return NULL;
  143.     }
  144.  
  145. FILE *open_file(fname, mode, path)
  146. char *fname;
  147. char *mode; /* right now, only mode "r" is supported */
  148. char *path; 
  149.     {
  150.     struct exfile *new;
  151.     struct compression_method *comp;
  152.  
  153.     if (mode[0] != 'r' || mode[1] != 0)
  154.         return NULL;
  155.     
  156.     if (ixfile == MAX_DESC)
  157.         return NULL;
  158.  
  159.     new = desc + ixfile++;
  160.  
  161.     fname = find_file(fname, path);
  162.     if (!fname)
  163.         return NULL;
  164.     for (comp = comp_table; comp->extension; comp++)
  165.         if (check_ext(fname, comp->extension))
  166.             {
  167.             char pipe[MAXPATHLEN + 25];
  168.  
  169.             sprintf(pipe, comp->command, fname);
  170.             new->type = PIPEFILE;
  171.             if (new->handle = popen(pipe, "r"))
  172.                 return new->handle;
  173.             else
  174.                 {
  175.                 ixfile--;
  176.                 return NULL;
  177.                 }
  178.             }
  179.     new->type = REALFILE;
  180.     if (new->handle = fopen(fname, "r"))
  181.         return new->handle;
  182.     else
  183.         {
  184.         ixfile--;
  185.         return NULL;
  186.         }
  187.     }
  188.  
  189.  
  190. void close_file(file)
  191. FILE *file;
  192.     {
  193.     if (file)
  194.         {
  195.         int i;
  196.  
  197.         for (i = 0; i < ixfile; i++)
  198.             if (desc[i].handle == file)
  199.                 {
  200.                 switch(desc[i].type)
  201.                     {
  202.                 case REALFILE:
  203.                     fclose(file);
  204.                     break;
  205.                 case PIPEFILE:
  206.                     pclose(file);
  207.                     break;
  208.                     }
  209.                 ixfile--;
  210.                 desc[i].handle = desc[ixfile].handle;
  211.                 desc[i].type = desc[ixfile].type;
  212.                 }
  213.             }
  214.     }
  215.  
  216.